package com.hhit.collaborative_learning.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;

import com.hhit.collaborative_learning.dao.BBS_ReplyDao;
import com.mysql.jdbc.StringUtils;

public class BBS_ReplyDaoImpl implements BBS_ReplyDao {

	@Override
	public ResultSet query(Connection con, int start, int end, String find,
			String order, String sort) {
		// TODO Auto-generated method stub
		String sql = "SELECT br.*,u.userNickName,u.userPoint,u.userTopicCount,u.userBestTopicCount,u.userIcon FROM collaborative_learning.bbs_reply as br left join collaborative_learning.user as u on u.userId=br.UserID";
		if (!StringUtils.isNullOrEmpty(find)) {
			sql += " where " + find;
		}
		if (!StringUtils.isNullOrEmpty(sort)) {
			sql += " order by " + sort;
			if (!StringUtils.isNullOrEmpty(order)) {
				sql += " " + order;
			}
		}
		if (end != 0 && start < end) {
			sql+=" limit "+start+","+end;
		}
		PreparedStatement prs;
		try {
			prs = con.prepareStatement(sql);
			return prs.executeQuery();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("非法请求：" + sql);
			return null;
		}
	}

	@Override
	public int getcount(Connection con, String find) {
		// TODO Auto-generated method stub
		int num = 0;
		String sql = "select count(*) from `collaborative_learning`.`bbs_reply`";
		if (!StringUtils.isNullOrEmpty(find)) {
			sql += " where " + find + " ";
		}
		try {
			PreparedStatement prs = con.prepareStatement(sql);
			ResultSet res = prs.executeQuery();
			if (res.next()) {
				num = res.getInt(1);
			}
		} catch (SQLException ex) {
			ex.printStackTrace();
		}
		return num;
	}

	@Override
	public int updateSupAndAgaPoint(Connection con, int sup, int aga,
			String find) {
		// TODO Auto-generated method stub
		String sql="UPDATE `collaborative_learning`.`bbs_reply` as r SET r.SupPoint = r.SupPoint+"+sup+",r.AgaPoint = r.AgaPoint+"+aga+" WHERE "+find;
		try {
			PreparedStatement prs = con.prepareStatement(sql);
			return prs.executeUpdate();
		} catch (SQLException ex) {
			ex.printStackTrace();
			System.out.println("非法sql语句  "+sql);
			return 0;
		} 
	}

	@Override
	public int deleteReply(Connection con, String find, String ids) {
		// TODO Auto-generated method stub
		String sql = "DELETE FROM collaborative_learning.bbs_reply WHERE "+find+" in ("+ids+")";
		try {
			PreparedStatement prs = con.prepareStatement(sql);
			return prs.executeUpdate();
		} catch (SQLException ex) {
			ex.printStackTrace();
			System.out.println("非法sql语句  "+sql);
			return 0;
		} 
	}

	@Override
	public int insert(Connection con, String text, String tid, String uid) {
		// TODO Auto-generated method stub
		String sql="INSERT INTO `collaborative_learning`.`bbs_reply` (`TopicID`,`Body`,`UserID`,`CreatedOn`) VALUES (?,?,?,?)";
		try {
			PreparedStatement prs = con.prepareStatement(sql);
			prs.setInt(1, Integer.valueOf(tid));
			prs.setString(2, text);
			prs.setInt(3, Integer.valueOf(uid));
			prs.setTimestamp(4, new Timestamp(System.currentTimeMillis()));
			return prs.executeUpdate();
		} catch (SQLException ex) {
			ex.printStackTrace();
			return 0;
		} 
	}

}
